import React from 'react';
import {App, Button, Card, Form, Input, InputNumber} from "antd";
import {GatewayRoute} from "type/entity/GatewayRoute";
import {InputMultiple} from "common/form";
import TextArea from "antd/es/input/TextArea";
import {filedArrayToStringObject} from "util/compare";
import RequestHandler from "http/RequestHandler";
import {useNavigate} from "react-router-dom";

const RouterAdd = () => {
    const navigate = useNavigate();
    const { message } = App.useApp();
    const onFinish = async (value: GatewayRoute) => {
        value.metadata = filedArrayToStringObject(value.metadata as any);
        const res = await RequestHandler.put<number>("/manager/admin/route", value);
        if (res.success && res.data > 0){
           message.success("提交修改成功");
           setTimeout(() => navigate("../"), 500);
        }
    };

    return (
        <Card title={"添加路由信息"}>
            <Form<GatewayRoute>
                name="dynamic_form_nest_item"
                onFinish={onFinish}
                labelCol={{span: 4}}
                wrapperCol={{span: 16}}
                autoComplete="off"
                initialValues={{order: 0}}
            >
                <Form.Item<GatewayRoute> name={"id"} label={"数据编号"} hidden>
                    <Input/>
                </Form.Item>
                <Form.Item<GatewayRoute> name={"routeId"} label={"路由ID/名字"}>
                    <Input/>
                </Form.Item>
                <Form.Item<GatewayRoute> name={"uri"} label={"路由地址"}>
                    <Input/>
                </Form.Item>
                <Form.Item<GatewayRoute> name={"order"} label={"路由排序"}>
                    <InputNumber/>
                </Form.Item>
                <InputMultiple<GatewayRoute> name={"metadata"} label={"工厂元数据"}/>
                <Form.Item<GatewayRoute> name={"remark"} label={"备注信息"}>
                    <TextArea rows={3}/>
                </Form.Item>
                <Form.Item wrapperCol={{span: 6, offset: 4}}>
                    <Button type="primary" htmlType="submit">
                        提交修改
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    );
};

export default RouterAdd;